package com.demo.service.sys.impl;

import com.demo.common.constant.api.base.BaseServiceImpl;
import com.demo.dao.common.entity.CatalogItem;
import com.demo.dao.sys.entity.SysCompanyItem;
import com.demo.service.sys.SysCatalogService;
import com.demo.service.sys.SysCompanyService;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysCompanyServiceImpl extends BaseServiceImpl<SysCompanyItem, Long>
        implements SysCompanyService {

    @Autowired
    SysCatalogService sysCatalogService;

    /**
     * 导入公司信息excel
     * @param file excel文件
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importExcel(MultipartFile file) throws Exception {
        if (file != null) {
            InputStream inputStream = file.getInputStream();
            Workbook workbook = WorkbookFactory.create(inputStream);
            Sheet sheet = workbook.getSheetAt(0);
            int rowNumber = sheet.getPhysicalNumberOfRows();
            SysCompanyItem sysCompanyItem = null;
            List<CatalogItem> catalogItemList = sysCatalogService.getCompanyTypeCatalogList();
            List<CatalogItem> catalogItems = new ArrayList<>();
            CatalogItem catalogItem = null;
            List<SysCompanyItem> companyItemList = new ArrayList<>();
            SysCompanyItem newCompany = null;
            for (int i = 1; i < rowNumber; i++) {
                Row row = sheet.getRow(i);
                //id
                Cell cell1 = row.getCell(1);
                if (!ObjectUtils.isEmpty(cell1.getNumericCellValue())) {
                    sysCompanyItem.setId(Long.parseLong(String.valueOf(cell1.getStringCellValue())));
                    Cell cell2 = row.getCell(2);
                    sysCompanyItem.setCname(cell2.getStringCellValue().trim());
                    Cell cell3 = row.getCell(3);
                    catalogItems = catalogItemList.stream().filter(item -> cell3.getStringCellValue().trim().equals(item.getLabel()))
                            .collect(Collectors.toList());
                    if(catalogItems.size() > 0){
                        sysCompanyItem.setTypeId(catalogItems.get(0).getId());
                    }
                    Cell cell4 = row.getCell(4);
                    sysCompanyItem.setAddress(cell4.getStringCellValue().trim());
                    Cell cell5 = row.getCell(5);
                    sysCompanyItem.setSortCode(cell5.getStringCellValue().trim());
                    sysCompanyItem.setUpdateTime(new Date());
                    newCompany = baseMapper.selectById(sysCompanyItem.getId());
                    if(ObjectUtils.isArray(newCompany)){
                        baseMapper.insert(sysCompanyItem);
                    }else{
                        baseMapper.update(sysCompanyItem);
                    }
                }
            }
        }
    }
}
